package edu.ncu.service.impl;

import edu.ncu.dao.CommentDao;
import edu.ncu.dao.PublishDao;
import edu.ncu.dao.UserDao;
import edu.ncu.entity.Comment;
import edu.ncu.entity.Publish;
import edu.ncu.entity.User;
import edu.ncu.service.PublishService;
import edu.ncu.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Slf4j(topic = "c.PublishServiceImpl")
@Service
public class PublishServiceImpl implements PublishService {
    @Autowired
    private PublishDao publishDao ;
    @Autowired
    private UserDao userDao ;
    @Autowired
    private CommentDao commentDao ;

//    @Autowired
//    private RedisUtil redisUtil ;

    // private String pattern = "user:*publish:*" ;

    /**
     * 根据用户的id查询用户所有的动态
     * 查询所有动态直接从数据库中查找
     * @param uId
     * @return
     */
    @Override
    public List<Publish> getPublishes(int uId) {
        List<Publish> publishes = null ;
        publishes = publishDao.getPublishes(uId) ;
//        String key = "user:"+uId+"publish:*" ;
//        Set<String> set = redisUtil.getPattern(key) ;
//
//        for (String s : set) {
//            Publish p = (Publish) redisUtil.get(s) ;
//            publishes.add(p) ;
//        }
//        if (publishes == null) {
//            // 说明缓存中
//        }
        return publishes ;
    }

    /**
     * 根据id查询动态
     * @param publishId
     * @return
     */
    @Override
    public Publish getPublishById(int publishId) {
        Publish publish = publishDao.getPublishById(publishId) ;
        if (publish == null) {
            return null ;
        } else {
            // 查询出publish对应的user
            User user = userDao.findByUserId(publish.getPUId()) ;
            publish.setUser(user);

            // 查询出publish对应的评论
            List<Comment> comments = commentDao.findPublishComment(publishId) ;
            // 查询出每条评论的评论人user
            for (Comment comment : comments) {
                User user1 = userDao.findByUserId(comment.getCommenterId()) ;
                comment.setUser(user1);

                // 查出 comments所有对应的子评论
                List<Comment> childList =  commentDao.findChildCommentOfPublish(comment.getCommentId()) ;
                // 查出所有子评论对应的user
                for (Comment child : childList) {
                    User user2 = userDao.findByUserId(child.getCommenterId()) ;
                    child.setUser(user2);
                }
                comment.setComments(childList);
            }
            publish.setComments(comments);


            // 封装publish对应的照片
            List<String> images = this.getImages(publish.getImg1(),publish.getImg2(),publish.getImg3()) ;
            publish.setImages(images);
        }

        return publish ;
    }

    /**
     * 添加一个动态
     * 将动态添加至redis缓存中
     * 键：user+userId+publish+publishId
     * user:1publish:1
     * @param p
     * @return
     */
    @Override
    public boolean addPublish(Publish p) {
        publishDao.insertPublish(p) ;
        return true ;
    }

    /**
     * 删除动态
     * 删除缓存中的动态
     * 删除数据库中的动态
     * @param publishId
     * @return
     */
    @Override
    public boolean deletePublish(int publishId) {
        boolean res  ;
        Publish p = publishDao.getPublishById(publishId) ;
        if (p != null) {
            res = publishDao.deletePublish(publishId) ;
            return res ;
        }
        return false ;
    }

    @Override
    public boolean updatePublish(int publishId) {
        Publish publish = publishDao.getPublishById(publishId) ;
        if (publish != null) {
            return publishDao.updatePublish(publishId) ; // 动态的评论数+1
        }
        return false ;
    }

    @Override
    public Publish updatePublishGreat(int publishId) {
        Publish publish = publishDao.getPublishById(publishId) ;
        if (publishDao.updatePublishGreat(publishId)) {
            return publish ;
        }
        return null;
    }

    @Override
    public List<Publish> getAllPublishes() {
        return publishDao.getAllPublishes();
    }

    /**
     * 将动态的三张照片封装成一个List集合，方便前端页面的展示
     * @param img1
     * @param img2
     * @param img3
     * @return
     */
    private List<String> getImages(String img1,String img2,String img3) {
        List<String> images = new ArrayList<>() ;
        boolean isEmpty = true ;
        if (img1 != null) {
            isEmpty = false ;
            images.add(img1) ;
        }
        if (img2 != null) {
            isEmpty = false ;
            images.add(img2) ;
        }
        if (img3 != null) {
            isEmpty = false ;
            images.add(img3) ;
        }
        return isEmpty == true ? null : images ;
    }
}














